<?php

require_once "Inweb/Controller/Action/Admin.php";

class Admin_ProfileController extends Inweb_Controller_Action_Admin
{
	public function initAdmin()
	{
		//for translate
		//$translate= Zend_Registry::get('translate');	
		//
		$this->_authAdmin = new Zend_Session_Namespace($this->_config->sessionAdminNamespace);
		$this->view->moduletitle="User Profile Data";
		unset($this->view->submenu);
		$this->checkAdminAccess('admin.system.user');
	}
	
	public function indexAction()
	{
		$controller='user';
		$action=$this->_getParam('action');
		// get form
		$form=$this->getAdminForm($this->getForm());
		
		if ( $_POST && $form->isValid($_POST) )
		{
				$values=$form->getValues();
				if ( empty($values['admin_password']) ) {
					unset($values['admin_password']);
					$password=$values['admin_password'];
				}else{
					$password=$this->_authAdmin->auth->password;
					//$this->_authAdmin->auth->password=$values['admin_password'];
				}
				$values['admingroup_id']=$this->_authAdmin->auth->admingroupid;
				$exe=$this->loadModel('user','admin')->edit($values,$this->_authAdmin->auth->id);
				$dbselect=$this->_db->select()->from($this->__admin)->where('admin_id=?',$this->_authAdmin->auth->id);
				$res=$this->_db->fetchRow($dbselect);
				if($res){		
					$this->_authAdmin->auth->email=$res['admin_email'];
					$this->_authAdmin->auth->name=$res['admin_name'];
					$this->_authAdmin->auth->username=$res['admin_username'];
					$this->_authAdmin->auth->password=$res['admin_password'];
					$this->_authAdmin->auth->admingroupid=$res['admingroup_id'];
					$this->redirect(array('action'=>'index'));
				}
		}
		
		$this->view->form=$form;
	}
	
	public function getForm()
	{
		//for translate
		//$translate= Zend_Registry::get('translate');	
		//
	
			$res= $this->_authAdmin->auth;
		
		$form = new Inweb_Form();
		
		// username
		$el=$form->createElement('text','admin_username');
		$el->setLabel($this->_translate->_('Username'))
			->setRequired(true)
			->setAttribs(array('size'=>45,'maxlength'=>15))
			->setValue($res->username)
			->addValidator('StringLength',false,array('min'=>5,'max'=>15))
			->addValidator('Alnum')
		;
		$form->addElement($el);
		
		// email
		$email=$form->createElement('text','admin_email');
		$email->setLabel($this->_translate->_('Email'))
			->setRequired(true)
			->setAttribs(array('size'=>45))
			->setValue($res->email)
			->addValidator('EmailAddress')
		;
		$form->addElement($email);
		
		// name
		$name=$form->createElement('text','admin_name');
		$name->setLabel($this->_translate->_('Name'))
			->setAttribs(array('size'=>35))
			->setValue($res->name)
		;
		$form->addElement($name);
		
		// group
		$group=$form->createElement('select','admingroup_id');
		$group->setLabel($this->_translate->_('Group'))
			->setMultiOptions($this->loadModel('group')->getOptions())
			->setAttribs(array('disabled'=>'disabled'))
			->setValue($res->admingroupid);
		;
		$form->addElement($group);
		
			// new password
			$npassword = $form->createElement('password','admin_password');
			$npassword->setLabel($this->_translate->_('New Password'))
			->addValidator('stringLength', false, array(6, 20))
			->addValidator('passwordconfirm',false, array('rpassword'))
			;
			$form->addElement($npassword);
			
			// retype password
			$rpassword = $form->createElement('password','rpassword');
			$rpassword->setLabel($this->_translate->_('Re-type Password'))
				->addValidator('stringLength', false, array(6, 20))
				->setIgnore(true)
			;
			$form->addElement($rpassword);
		
		return $form;
	}
}